<?php namespace Admin\Controller; 
/**
 * 文章控制器
 *
 */
class ArticleController extends CommonController{
		private $article = null;
		public function __init(){
				parent::__init();
				$this->article = new \Common\Model\Article;
		}	
		//显示页面
		public function index(){
				//获取分页内容
				$where = array('is_recycle' => 0);
				$total = $this->article->where($where)->count();
				$page = Page::row(8)->make($total);
				$arcData = $this->article->join('category','category_cid','=','cid')->field('aid,title,author,sendtime,cname')->where($where)->orderBy('sendtime','DESC')->limit(Page::limit())->get();
				//分配分页和旧数据
				
				$this->assign('page',$page);
				$this->assign('total',$total);
				$this->assign('arcData',$arcData);
				$this->display();
		}
	
		//添加文章
		public function add(){
				//表单数据提交处理
				if (IS_POST) {
						if ($this->article->store()) {
								$this->success('添加成功',U('index'));
						}
						p($this->article->getError());die;
						$this->error($this->article->getError());
				}
				//查询所有的分类
				$category = new \Common\Model\Category();
				$data = Data::tree($category->field('cid,cname,pid')->get(),'cname');
				$this->assign('data',$data);
				//查询所有的标签
				$tag = new \Common\Model\Tag();
				$this->assign('tag',$tag->get());
				$this->display();
		}
	
		//编辑文章
		public function edit(){
					//修改数据
					if (IS_POST) {
						if ($this->article->edit(Q('aid',0,'intval'))) {
							 $this->success('编辑成功,马上为您跳转...',U('index'));
						}
						$this->error($this->article->getError());
					}
					
				   	$aid = Q('get.aid',0,'intval');
				   	//查询出所有的旧数据
				   	$field = 'aid,title,thumb,digest,author,category_cid, keywords,des,content,imgs';
				   	//查询出所有的数据
			   		$oldData = $this->article->join('article_data','article_aid','=','aid')->field($field)->where(array('aid' => $aid))->find();
			   		$this->assign('oldData', $oldData);
			   		//文章所对应的标签（获得一维数组）
			   		$tids = (new \Common\Model\ArticleTag)->where("article_aid={$aid}")->lists('tag_tid');
					if (is_null($tids)) {
							$tids = array();
							$this->assign('tids',$tids);
					}else{
							$this->assign('tids',$tids);
					}
					
					//查询所有的分类
				   	$category = new \Common\Model\Category();
				   	$data = Data::tree($category->field('cid,cname,pid')->get(),'cname');
				   	$this->assign('data',$data);
				   	//查询所有的标签
				   	$tagModel = new \Common\Model\Tag();
				   	$this->assign('tag',$tagModel->get());
				   	
					$this->display();
		}
	
		//删除文章到回收站
		public function del(){
				$aid = Q('get.aid',0,'intval');
				$this->article->where(array('aid'=>$aid))->save(array('is_recycle' => 1));
				//删除成功,调至首页
				$this->success('删除成功,正在为您跳转...',U('index'));
		}
	
		//文章回收列表
		public function recycle(){
				//获取分页内容
				$where = array('is_recycle' => 1);
				$total = $this->article->where($where)->count();
				$page = Page::row(8)->make($total);
				$arcData = $this->article->join('category','category_cid','=','cid')->field('aid,title,author,sendtime,cname')->where($where)->orderBy('sendtime','DESC')->limit(Page::limit())->get();
				//分配分页和旧数据
				$this->assign('page',$page);
				$this->assign('total',$total);
				$this->assign('arcData',$arcData);
				$this->display();
		}
		//还原
		public function recover(){
				$aid = Q('get.aid',0,'intval');
				$this->article->where(array('aid'=>$aid))->save(array('is_recycle' => 0));
				//删除成功,调至首页
				$this->success('还原成功,正在为您跳转...',U('recycle'));
		}
		//彻底删除
		public function remove(){
			$this->article->del(Q('get.aid',0,'intval'));
			$this->success('删除成功,正在为您跳转...',U('recycle'));
		}
}

?>
